﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.Revit
{
	public class HYCreatView3DSection
	{
		public HYCreatView3DSection(Autodesk.Revit.DB.Document document, PickedBox pickBox)
		{
			this.doc = document;
			this.pBox = pickBox;
			this.IntialProperty();
		}

		private void IntialProperty()
		{
			Level genLevel = this.doc.ActiveView.GenLevel;
			this.bottomElev = this.GetLevelElevation(genLevel);
			if (genLevel.Elevation < this.GetUpLevel(genLevel).Elevation)
			{
				this.topElev = this.GetUpLevel(genLevel).Elevation;
				return;
			}
			this.topElev = this.GetUpLevel(genLevel).Elevation + 9.84251968503937;
		}

		private double GetLevelElevation(Level level)
		{
			return level.Elevation;
		}

		private Level GetUpLevel(Level bLevel)
		{
			Level result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.OfClass(typeof(Level));
			FilteredElementIterator elementIterator = filteredElementCollector.GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				Level level = element as Level;
				if (level != null)
				{
					if (bLevel.Elevation == level.Elevation)
					{
						result = level;
					}
					if (bLevel.Elevation < level.Elevation)
					{
						result = level;
						break;
					}
				}
			}
			return result;
		}

		private void CompareMaxMinPoint()
		{
			this.minX = this.pBox.Min.X;
			this.maxX = this.pBox.Max.X;
			if (this.minX > this.maxX)
			{
				this.minX = this.pBox.Max.X;
				this.maxX = this.pBox.Min.X;
			}
			this.minY = this.pBox.Min.Y;
			this.maxY = this.pBox.Max.Y;
			if (this.minY > this.maxY)
			{
				this.minY = this.pBox.Max.Y;
				this.maxY = this.pBox.Min.Y;
			}
		}

		public View3D CreatHYSubView3D(bool useDefault3dViewName, bool beLockView)
		{
			double num = this.topElev * Math.Sqrt(2.0) / 2.0;
			this.CompareMaxMinPoint();
			BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ();
			boundingBoxXYZ.Min = new XYZ(this.minX, this.minY, this.bottomElev);
			boundingBoxXYZ.Max = new XYZ(this.maxX, this.maxY, this.topElev);
			XYZ eye = new XYZ((this.maxX + this.minX) / 3.0, (this.maxY + this.minY) / 3.0, (this.bottomElev + this.topElev) / 3.0);
			View3D view3D = this.doc.CreatHYSubView3D(new XYZ(1.0, 1.0, -1.0), eye);
			if (view3D == null)
			{
				return null;
			}
			view3D.SetYJKSectionBox(boundingBoxXYZ);
			this.Set3dViewName(this.doc, view3D, useDefault3dViewName, this.view3DName);
			if (beLockView)
			{
				view3D.HYSaveOrientationAndLock();
			}
			this.SetViewDiscipline(view3D);
			return view3D;
		}

		private void SetViewDiscipline(View3D view3D)
		{
			Parameter parameter = this.doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_DISCIPLINE);
			int num = parameter.AsInteger();
			view3D.get_Parameter(BuiltInParameter.VIEW_DISCIPLINE).Set(num);
			parameter = this.doc.ActiveView.GetParameter("子规程");
			if (parameter != null && parameter.HasValue)
			{
				string text = parameter.AsString();
				view3D.GetParameter("子规程").Set(text);
			}
			Parameter parameter2 = view3D.get_Parameter(BuiltInParameter.VIEWER_MODEL_CLIP_BOX_ACTIVE);
		}

		private void Set3dViewName(Autodesk.Revit.DB.Document doc, View3D view3d, bool useDefault3dViewName, string view3DName)
		{
			if (useDefault3dViewName)
			{
				FilteredElementIterator elementIterator = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
				elementIterator.Reset();
				while (elementIterator.MoveNext())
				{
					Element element = elementIterator.Current;
					Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					if (view != null && !view.IsTemplate && (int)view.ViewType == 4 && view.Name.CompareTo(view3DName) == 0)
					{
						doc.DeleteElement(view);
						break;
					}
				}
				view3d.Name = view3DName;
				return;
			}
			List<int> list = new List<int>();
			FilteredElementIterator elementIterator2 = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
			elementIterator2.Reset();
			while (elementIterator2.MoveNext())
			{
				Element element2 = elementIterator2.Current;
				Autodesk.Revit.DB.View view2 = element2 as Autodesk.Revit.DB.View;
				if (view2 != null && !view2.IsTemplate && (int)view2.ViewType == 4)
				{
					string name = view2.Name;
					int num = name.LastIndexOf(view3DName);
					if (num > -1)
					{
						string value = name.Substring(num + view3DName.Length);
						try
						{
							int item = Convert.ToInt32(value);
							list.Add(item);
						}
						catch (Exception)
						{
						}
					}
				}
			}
			list.Sort();
			int num2 = 0;
			if (list.Count > 0)
			{
				num2 = list[list.Count - 1];
			}
			view3d.Name = view3DName + (num2 + 1).ToString();
		}

		private Document doc;

		private double bottomElev;

		private double topElev;

		private PickedBox pBox;

		private string view3DName = "局部三维视图";

		private double minX;

		private double maxX;

		private double minY;

		private double maxY;
	}
}
